//=============================================================================
// SWDA_BattleCommandMenu.js
//=============================================================================
/*:
* @plugindesc 枫之舞界面 - 战斗命令窗口
* @author 老饕
* @version 1.0.0
* @date 2020.1.27

 * @param Actor Command Button Image
 * @desc 角色命令按钮图片（攻击、奇术、物品、行动）
 * @default MenuButton_ActorCommand
 
 * @param Action Command Button Image
 * @desc 行动指令按钮图片（防御、逃跑）
 * @default MenuButton_ActionCommand
*/

var SWDA_BattleCommandMenu = SWDA_BattleCommandMenu || {};
var SWDAWindowManager = SWDAWindowManager || {};
var parameters = PluginManager.parameters('SWDA_BattleCommandMenu');

SWDAWindowManager.actorCommandButtonImage  = String(parameters['Actor Command Button Image']   || 'MenuButton_ActorCommand');
SWDAWindowManager.actionCommandButtonImage  = String(parameters['Action Command Button Image']   || 'MenuButton_ActionCommand');

//=============================================================================
// 【一】【角色指令窗口】
//=============================================================================
//-----------------------------------------------------------------------------
// Window_ActorCommand
//
// The window for selecting an actor's action on the battle screen.

function Window_ActorCommand() {
    this.initialize.apply(this, arguments);
}

Window_ActorCommand.prototype = Object.create(SWDA_Window_Command_4Arrows.prototype);
Window_ActorCommand.prototype.constructor = Window_ActorCommand;

//--------------------------------------------------------
// 初始化
Window_ActorCommand.prototype.initialize = function() {
    //var y = Graphics.boxHeight - this.windowHeight();
    SWDA_Window_Command_4Arrows.prototype.initialize.call(this, 0, 0);
    this.openness = 0;
    this.deactivate();
    this._actor = null;

	
	
};

Window_ActorCommand.prototype.windowWidth = function() {
    return 192;
};

Window_ActorCommand.prototype.numVisibleRows = function() {
    return 4;
};
//--------------------------------------------------------
// 添加命令
Window_ActorCommand.prototype.makeCommandList = function() {
    if (this._actor) {
        this.addAttackCommand();
        this.addSkillCommands();
		this.addActionCommand();
		this.addItemCommand();
    }
};

//攻击（未变）
Window_ActorCommand.prototype.addAttackCommand = function() {
    this.addCommand(TextManager.attack, 'attack', this._actor.canAttack());
};
//技能（合并为1项）
Window_ActorCommand.prototype.addSkillCommands = function() {
    this.addCommand(TextManager.skill, 'skill', true);
};
/*//去掉防御
Window_ActorCommand.prototype.addGuardCommand = function() {
    this.addCommand(TextManager.guard, 'guard', this._actor.canGuard());
};*/
//物品（未变）
Window_ActorCommand.prototype.addItemCommand = function() {
    this.addCommand(TextManager.item, 'item');
};
//新增行动
Window_ActorCommand.prototype.addActionCommand = function() {
	this.addCommand('行动', 'action', true);
};
//--------------------------------------------------------
// SETUP（增加按钮）
Window_ActorCommand.prototype.setup = function(actor) {
    this._actor = actor;
    this.clearCommandList();
    this.makeCommandList();
    this.refresh();
    this.selectLast();
    this.activate();
    this.open();
	if (!this._buttons)
		this.createButtons(SWDAWindowManager.actorCommandButtonImage);
};

//--------------------------------------------------------
// 按键响应（未改）
Window_ActorCommand.prototype.processOk = function() {
    if (this._actor) {
        if (ConfigManager.commandRemember) {
            this._actor.setLastCommandSymbol(this.currentSymbol());
        } else {
            this._actor.setLastCommandSymbol('');
        }
    }
    Window_Command.prototype.processOk.call(this);
};

//--------------------------------------------------------
// 指令记忆（去掉技能分类）
Window_ActorCommand.prototype.selectLast = function() {
    this.select(0);

    if (this._actor && ConfigManager.commandRemember) {
        var symbol = this._actor.lastCommandSymbol();
        this.selectSymbol(symbol);
		/*
        if (symbol === 'skill') {
            var skill = this._actor.lastBattleSkill();
            if (skill) {
                this.selectExt(skill.stypeId);
            }
        }
		*/
    }
};
// 激活即打开
SWDA_BattleCommandMenu.WindowActorCommand_Activate = Window_ActorCommand.prototype.activate;
Window_ActorCommand.prototype.activate = function() {
    SWDA_BattleCommandMenu.WindowActorCommand_Activate.call(this);
	this.open();
};
//=============================================================================
// 【二】Scene_Battle新增行动命令窗口（防御、逃跑）
//=============================================================================

// 创建命令窗口
SWDA_BattleCommandMenu.SceneBattle_CreateAllWindows = Scene_Battle.prototype.createAllWindows;
Scene_Battle.prototype.createAllWindows = function() {
	SWDA_BattleCommandMenu.SceneBattle_CreateAllWindows.call(this);
    this.createActionCommandWindow();
};

// 变更Handler
Scene_Battle.prototype.createActorCommandWindow = function() {
    this._actorCommandWindow = new Window_ActorCommand();
    this._actorCommandWindow.setHandler('attack', this.commandAttack.bind(this));
    this._actorCommandWindow.setHandler('skill',  this.commandSkill.bind(this));
    this._actorCommandWindow.setHandler('item',   this.commandItem.bind(this));
	this._actorCommandWindow.setHandler('action',  this.commandAction.bind(this));
    this.addWindow(this._actorCommandWindow);
};
Scene_Battle.prototype.commandAction = function() {
	
    this._actionCommandWindow.refresh();
    this._actionCommandWindow.show();
    this._actionCommandWindow.activate();
	this.startActionCommandSelection();
};

Scene_Battle.prototype.createActionCommandWindow = function() {
    this._actionCommandWindow = new Window_ActionCommand();
    this._actionCommandWindow.setHandler('guard',  this.commandGuard.bind(this));
    this._actionCommandWindow.setHandler('escape', this.commandEscape.bind(this));
	this._actionCommandWindow.setHandler('cancel', this.startActorCommandSelection.bind(this));
    this._actionCommandWindow.deselect();
    this.addWindow(this._actionCommandWindow);
};



// 补充关于Action窗口的开启/关闭
// 判断是否有“输入指令的”窗口激活
SWDA_BattleCommandMenu.SceneBattle_IsAnyInputWindowActive = Scene_Battle.prototype.isAnyInputWindowActive;
Scene_Battle.prototype.isAnyInputWindowActive = function() {
    return (SWDA_BattleCommandMenu.SceneBattle_IsAnyInputWindowActive.call(this) ||
			this._actionCommandWindow.active);
};
// 开始角色指令窗口
SWDA_BattleCommandMenu.SceneBattle_StartActorCommandSelection = Scene_Battle.prototype.startActorCommandSelection;
Scene_Battle.prototype.startActorCommandSelection = function() {
	this._actionCommandWindow.close();
	SWDA_BattleCommandMenu.SceneBattle_StartActorCommandSelection.call(this);
};

SWDA_BattleCommandMenu.SceneBattle_Stop = Scene_Battle.prototype.stop;
Scene_Battle.prototype.stop = function() {
	SWDA_BattleCommandMenu.SceneBattle_Stop.call(this);
    this._actionCommandWindow.close();
};

SWDA_BattleCommandMenu.SceneBattle_UpdateStatusWindow = Scene_Battle.prototype.updateStatusWindow;
Scene_Battle.prototype.updateStatusWindow = function() {
	SWDA_BattleCommandMenu.SceneBattle_UpdateStatusWindow.call(this);
    if ($gameMessage.isBusy()) {
        this._actionCommandWindow.close();
    }
};

/*
SWDA_BattleCommandMenu.SceneBattle_StartActionCommandSelection = Scene_Battle.prototype.startActionCommandSelection;
Scene_Battle.prototype.startActionCommandSelection = function() {
    SWDA_BattleCommandMenu.SceneBattle_StartActionCommandSelection.call(this);
    this._partyCommandWindow.close();
	this._actionCommandWindow.open();
};
*/
Scene_Battle.prototype.startActionCommandSelection = function() {
    this._statusWindow.select(BattleManager.actor().index());
    this._partyCommandWindow.close();
	this._actorCommandWindow.close();
	//this._actionCommandWindow.open();
    this._actionCommandWindow.setup(BattleManager.actor());
};
SWDA_BattleCommandMenu.SceneBattle_EndCommandSelection = Scene_Battle.prototype.endCommandSelection;
Scene_Battle.prototype.endCommandSelection = function() {
	this._actionCommandWindow.close();
	SWDA_BattleCommandMenu.SceneBattle_EndCommandSelection.call(this);
	
	
};

Scene_Battle.prototype.commandEscape = function() {
    BattleManager.processEscape();
    //this.changeInputWindow();
	if (BattleManager.isInputting()) {
		this.startPartyCommandSelection();
	}else{
		this.endCommandSelection();
	}
};

//=============================================================================
// 【攻击、选择等情况隐藏窗口】

SWDA_BattleCommandMenu.Scene_Battle_CommandAttack = Scene_Battle.prototype.commandAttack;
Scene_Battle.prototype.commandAttack = function() {
	this._actorCommandWindow.close();
	SWDA_BattleCommandMenu.Scene_Battle_CommandAttack.call(this);
};

SWDA_BattleCommandMenu.Scene_Battle_CommandSkill = Scene_Battle.prototype.commandSkill;
Scene_Battle.prototype.commandSkill = function() {
	this._actorCommandWindow.close();
    SWDA_BattleCommandMenu.Scene_Battle_CommandSkill.call(this);
	
};


SWDA_BattleCommandMenu.Scene_Battle_CommandGuard = Scene_Battle.prototype.commandGuard;
Scene_Battle.prototype.commandGuard = function() {
	this._actorCommandWindow.close();
	SWDA_BattleCommandMenu.Scene_Battle_CommandGuard.call(this);
};


SWDA_BattleCommandMenu.Scene_Battle_CommandItem = Scene_Battle.prototype.commandItem;
Scene_Battle.prototype.commandItem = function() {
	this._actorCommandWindow.close();
    SWDA_BattleCommandMenu.Scene_Battle_CommandItem.call(this);
	
};

/*
SWDA_BattleCommandMenu.Scene_Battle_OnEnemyCancel = Scene_Battle.prototype.onEnemyCancel;
Scene_Battle.prototype.onEnemyCancel = function() {
	SWDA_BattleCommandMenu.Scene_Battle_OnEnemyCancel.call(this);
    switch (this._actorCommandWindow.currentSymbol()) {
    case 'attack':
        this._actorCommandWindow.open();
        break;
    }
};

SWDA_BattleCommandMenu.Scene_Battle_OnSkillCancel = Scene_Battle.prototype.onSkillCancel;
Scene_Battle.prototype.onSkillCancel = function() {
	SWDA_BattleCommandMenu.Scene_Battle_OnSkillCancel.call(this);
    this._actorCommandWindow.open();
};

SWDA_BattleCommandMenu.Scene_Battle_OnItemCancel = Scene_Battle.prototype.onItemCancel;
Scene_Battle.prototype.onItemCancel = function() {
    SWDA_BattleCommandMenu.Scene_Battle_OnItemCancel.call(this);
	this._actorCommandWindow.open();
};
*/
//=============================================================================
// 【三】【行动指令窗口】（参照Window_PartyCommand修改）
//=============================================================================
//-----------------------------------------------------------------------------
// Window_ActionCommand
//
// The window for selecting whether to fight or escape on the battle screen.

function Window_ActionCommand() {
    this.initialize.apply(this, arguments);
}

Window_ActionCommand.prototype = Object.create(SWDA_Window_Command_4Arrows.prototype);
Window_ActionCommand.prototype.constructor = Window_ActionCommand;

Window_ActionCommand.prototype.initialize = function() {
    var y = Graphics.boxHeight - this.windowHeight();
    SWDA_Window_Command_4Arrows.prototype.initialize.call(this, 0, y);
    this.openness = 0;
    this.deactivate();
};

Window_ActionCommand.prototype.windowWidth = function() {
    return 192;
};

Window_ActionCommand.prototype.numVisibleRows = function() {
    return 2;
};

Window_ActionCommand.prototype.makeCommandList = function() {
	if (this._actor) {
		this.addCommand('', '', false);
		this.addCommand(TextManager.guard, 'guard', this._actor.canGuard());
		this.addCommand('', '', false);
		this.addCommand(TextManager.escape, 'escape', BattleManager.canEscape());
	}
};

Window_ActionCommand.prototype.setup = function(actor) {
	this._actor = actor;
    this.clearCommandList();
    this.makeCommandList();
    this.refresh();
    this.select(1);
    this.activate();
    this.open();
	if (!this._buttons)
		this.createButtons(SWDAWindowManager.actionCommandButtonImage);
    
};




//=============================================================================
// 更改战斗窗口外观、位置（临时）
//=============================================================================
Window_SkillList.prototype.maxCols = function() {
    return 1;
};

Window_SkillList.prototype.windowWidth = function() {
    return 512;
};

Window_ItemList.prototype.maxCols = function() {
    return 1;
};
// Window_ItemList.prototype.windowWidth = function() {
//     return 512;
// };

// 【三】【暂时取消技能分类筛选，直接显示所有技能】

Window_SkillList.prototype.makeItemList = function() {
    if (this._actor) {
        this._data = this._actor.skills();
    } else {
        this._data = [];
    }
};

Scene_Battle.prototype.createSkillWindow = function() {
    var wy = this._helpWindow.y + this._helpWindow.height;
    var wh = this._statusWindow.y - wy;
    this._skillWindow = new Window_BattleSkill(640, wy, 512, wh);
    this._skillWindow.setHelpWindow(this._helpWindow);
    this._skillWindow.setHandler('ok',     this.onSkillOk.bind(this));
    this._skillWindow.setHandler('cancel', this.onSkillCancel.bind(this));
    this.addWindow(this._skillWindow);
};

Scene_Battle.prototype.createItemWindow = function() {
    var wy = this._helpWindow.y + this._helpWindow.height;
    var wh = this._statusWindow.y - wy;
    this._itemWindow = new Window_BattleItem(640, wy, 512, wh);
    this._itemWindow.setHelpWindow(this._helpWindow);
    this._itemWindow.setHandler('ok',     this.onItemOk.bind(this));
    this._itemWindow.setHandler('cancel', this.onItemCancel.bind(this));
    this.addWindow(this._itemWindow);
};